package my.senix.sc.controller;

import org.springframework.core.env.Environment;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * Created by lhs on 2018/11/7.
 */
@RestController
public class DbController {

    private JdbcTemplate jdbcTemplate;

    private Environment env;

    public DbController(JdbcTemplate jdbcTemplate, Environment env){
        this.jdbcTemplate=jdbcTemplate;
        this.env=env;
    }

    @GetMapping("/envs")
    public Map<String,String> getEnv(@RequestParam("p") String params){
        String[] splits = params.split(",");

        Map<String,String> map=new HashMap<>();
//        return Stream.of(splits)
//                .collect(Collectors.toMap(String::toString,k->env.getProperty(k)));

        for(String s:splits){
            map.put(s,env.getProperty(s));
        }

        return map;

    }

    @GetMapping("/table/{name}")
    public List<Map<String,Object>> getData(@PathVariable String name){

        return jdbcTemplate.queryForList("select * from "+name);
    }
}
